<?php

namespace Entities;

use Doctrine\ORM\Mapping as ORM;

/**
 * Entities\Image
 */
class Image
{
    /**
     * @var string $filename
     */
    private $filename;

    /**
     * @var string $filelocation
     */
    private $filelocation;

    /**
     * @var integer $id
     */
    private $id;


    private $exif = array();

    private $latitude = null;

    private $longitude = null;



    /**
     * Set filename
     *
     * @param string $filename
     * @return Image
     */
    public function setFilename($filename)
    {
        $this->filename = $filename;
        return $this;
    }

    /**
     * Get filename
     *
     * @return string
     */
    public function getFilename()
    {
        return $this->filename;
    }


    /**
     *
     *
     *
     */
    public function getThumbnailFilename()
    {
        return preg_replace('/\./i', '_thumb.', $this->filename);
    }

    /**
     * Set filelocation
     *
     * @param string $filelocation
     * @return Image
     */
    public function setFilelocation($filelocation)
    {
        $this->filelocation = $filelocation;
        return $this;
    }

    /**
     * Get filelocation
     *
     * @return string
     */
    public function getFilelocation()
    {
        return $this->filelocation;
    }


    /**
     *
     *
     *
     */
    public function getThumbnailFilelocation()
    {
        return $this->filelocation;
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }



    /**
     * private function getExifData
     * Will read the exif data off the image object and add it to the array property $exif.
     *
     * @author        Jeroen Sen <jeroensen@22vision.nl>
     * @version       20120925
     * @param         void
     * @return        array
     *
     */
    private function getExifData()
    {
        $this->exif = exif_read_data($this->filelocation . DIRECTORY_SEPARATOR . $this->filename, false, true);

    } // private function getExifData()


    /**
     * public function get GpsData
     * Will return the coords for the image object.
     *
     * @author        Jeoren Sen <jeroensen@2vision.nl>
     * @version       20120925
     * @param         void
     * @return        Array
     *
     */
    public function getGpsData()
    {
        $coords = array( 'latitude' => null, 'longitude' => null);

        if (!isset($this->exit) || !is_array($this->exif)) {
            $this->getExifData();
        }


        if (isset($this->exif) && is_array($this->exif) && array_key_exists('GPS', $this->exif)) {
            $coords['latitude'] = $this->latitude = $this->getGpsCoords($this->exif['GPS']['GPSLatitude'], $this->exif['GPS']['GPSLatitudeRef']);
            $coords['longitude'] = $this->longitude = $this->getGpsCoords($this->exif['GPS']['GPSLongitude'], $this->exif['GPS']['GPSLongitudeRef']);
        }

        return $coords;

    } // public function getGpsData()



    /**
     * private function getGpsCoords
     * Will nicely format the Gps coords to a float.
     *
     * @author        Jeroen Sen <jeroensen@22vision.nl>
     * @version       20120925
     * @param         Array $exifCoord
     * @param         String $hemi, the hemisphere on which the image was created
     * @return        Float
     *
     */
    private function getGpsCoords($exifCoord, $hemi) {

        $degrees = count($exifCoord) > 0 ? $this->gps2Num($exifCoord[0]) : 0;
        $minutes = count($exifCoord) > 1 ? $this->gps2Num($exifCoord[1]) : 0;
        $seconds = count($exifCoord) > 2 ? $this->gps2Num($exifCoord[2]) : 0;

        $flip = ($hemi == 'W' or $hemi == 'S') ? -1 : 1;

        return $flip * ($degrees + $minutes / 60 + $seconds / 3600);

    } // private function getGpsCoords($exifCoord, $hemi)



    /**
     * private function gps2Num
     *
     * @author        Jeroen Sen <jeroensen@22vision.nl>
     * @version       20120925
     * @param         Array $coordPart
     * @return        Float
     */
    private function gps2Num($coordPart)
    {

        $parts = explode('/', $coordPart);

        if (count($parts) <= 0)
            return 0;

        if (count($parts) == 1)
            return $parts[0];

        return floatval($parts[0]) / floatval($parts[1]);

    } // private function gps2Num($coordPart)



    /**
     * public function getAddress
     * Will convert a latitude and a longitude into an textual representation of an address.
     *
     * @author        Jeroen Sen <jeroensen@22vision.nl>
     * @version       20120925
     * @param         float $latitude
     * @param         float $longitude
     * @return        string $address
     *
     */
    public function getAddress($latitude, $longitude)
    {
        $address = '';

        if ((isset($latitude) && is_float($latitude)) && (isset($longitude) && is_float($longitude))) {

            // do not add your key (apperently)
            $url = 'http://maps.googleapis.com/maps/api/geocode/json?v=3&language=nl&latlng='.$latitude.','.$longitude.'&sensor=false';
            $source = file_get_contents($url);
            $obj = json_decode($source);

            $address = $obj->results[0]->formatted_address;

        } // if ((isset($latitude) && is_float($latitude)) && (isset($longitude) && is_float($longitude)))

        return $address;

    } // public function getAddress($latitude, $longitude)



    /**
     *
     *
     *
     */
    public function getDimensions()
    {
        return getimagesize($this->filelocation . DIRECTORY_SEPARATOR . $this->filename);

    } // public function getDimensions



}